<script setup lang="ts">


/////////
import {onMounted, ref} from 'vue';
import {useRouter} from "vue-router";
import teamAxios from "../plugins/teamAxios";
import pdx from "../assets/pdx.png"
import {showToast} from "vant";
import {teamType} from "../model/team";
import {getCurrentUser} from "../services/user.ts";
import {userType} from "../model/user";
import {teamStatusEnum} from "../constants/team.ts";

const teamList = ref<teamType[]>([]);
onMounted(async () => {
  const res = await teamAxios.get('/myteam')
  // @ts-ignore
  if (res?.code === 200 && res.data) {
    console.log("list成功")
    teamList.value = res.data;
  } else {
    console.log("list失败")
  }
})
const user = ref<userType>();
onMounted(async () => {
  const res = await getCurrentUser();
  // @ts-ignore
  if (res.code === 200 && res.data !== null) {
    user.value = res.data
  }
})

//更新
const router = useRouter();
const doUpdate = (id: number) => {
  console.log("发送" + id);
  router.push({
    path: '/team/update',
    query: {
      id
    }
  })
}

const doDeleteTeam = async (id: number) => {
  const res = await teamAxios.post("/delete", {
    id: id
  })
  // @ts-ignore
  if (res?.code === 200 && res.data) {
    console.log("加入成功")

    const res = await teamAxios.get('/list')
    // @ts-ignore
    if (res?.code === 200 && res.data) {
      console.log("list成功")
      showToast('解散成功');
      teamList.value = res.data;
    } else {
      console.log("list失败")
    }
  } else {
    console.log("加入失败")
    showToast('解散失败');
  }
}
const doQuitTeam = async (id: number) => {
  const res = await teamAxios.post("/quit", {
    id: id
  })
  // @ts-ignore
  if (res?.code === 200 && res.data) {
    console.log("加入成功")
    const res = await teamAxios.get('/list')
    // @ts-ignore
    if (res?.code === 200 && res.data) {
      console.log("list成功")
      showToast('退出成功');
      teamList.value = res.data;
    } else {
      console.log("list失败")
    }
  } else {
    console.log("退出失败")
    showToast('退出失败');
  }
}

</script>

<template>
  <van-cell title="新建我的队伍" is-link to="/team/add" style="padding-top: 56px"/>
  <van-card
      v-for="team in teamList"
      :thumb="pdx"
      :title="team.teamName"
      :desc="team.teamDescription"
      :tag="team.userId === user?.id ? '队长' : ''"
  >
  <template #tags>
    <van-tag plain type="primary" style="margin-right: 4px">{{  teamStatusEnum[team.teamStatus]  }}</van-tag>
  </template>
  <template #bottom>
    <div>
      {{ `队伍人数:0/${team.teamMaxnumber}` }}
    </div>
    <div v-if="team.teamExpiretime">
      {{ '过期时间: ' + team.teamExpiretime }}
    </div>
    <div>
      {{ '创建时间: ' + team.createTime }}
    </div>
  </template>
  <template #footer>
    <van-button @click="doUpdate(team.id)" v-if="team.userId === user?.id" size="mini">更新队伍</van-button>
    <van-button @click="doQuitTeam(team.id)" size="mini" type="warning">退出队伍</van-button>
    <van-button @click="doDeleteTeam(team.id)" v-if="team.userId === user?.id" size="mini" type="danger">解散队伍</van-button>
  </template>
  </van-card>
  <van-divider>小主，已经到底了</van-divider>
  <van-empty style="padding-top: 106px" v-if="teamList.length<1" description="结果为空"/>

</template>

<style scoped>

</style>